package stack;

import org.junit.Test;

import java.util.Stack;

/**
 * @className: LeetCode_739
 * @Description: ...
 * @author: kunkun
 * @date: 2025/3/12 22:54
 */
public class LeetCode_739 {

    public int[] dailyTemperatures(int[] temperatures) {
        return solution(temperatures);
    }

    @Test
    public void test(){
        for (int i : dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73})) {
            System.out.print(i+" ");
        }   //1,1,4,2,1,1,0,0
    }

    public int[] solution(int[] temperatures){
        int[] results = new int[temperatures.length];
        //新建单调栈
        Stack<Integer> stack = new Stack<>();
        stack.add(0);
        for (int i = 1; i < temperatures.length; i++) {
            while (stack.size()>0 && temperatures[i]>temperatures[stack.peek()]){
                Integer num = stack.pop();
                results[num]=i-num;
            }
            stack.push(i);
        }
        while (stack.size()!=0){
            Integer num = stack.pop();
            results[num]=0;
        }
        return results;

    }


}
